package com.ruoyi.interview.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.interview.domain.InterviewQuestion;
import com.ruoyi.interview.domain.InterviewRecord;
import com.ruoyi.interview.domain.vo.DeepQuestionVo;
import com.ruoyi.interview.domain.vo.DeepSeekResponseData;
import com.ruoyi.interview.mapper.InterviewQuestionMapper;
import com.ruoyi.interview.mapper.InterviewRecordMapper;
import com.ruoyi.interview.service.IInterviewRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 模拟面试记录Service业务层处理
 * 
 * @author lishugen
 * @date 2025-11-23
 */
@Service
public class InterviewRecordServiceImpl implements IInterviewRecordService 
{
    @Autowired
    private InterviewRecordMapper interviewRecordMapper;

    @Autowired
    private InterviewQuestionMapper interviewQuestionMapper;

    /**
     * 查询模拟面试记录
     * 
     * @param id 模拟面试记录主键
     * @return 模拟面试记录
     */
    @Override
    public InterviewRecord selectInterviewRecordById(Long id)
    {
        return interviewRecordMapper.selectInterviewRecordById(id);
    }

    /**
     * 查询模拟面试记录列表
     * 
     * @param interviewRecord 模拟面试记录
     * @return 模拟面试记录
     */
    @Override
    public List<InterviewRecord> selectInterviewRecordList(InterviewRecord interviewRecord)
    {
        return interviewRecordMapper.selectInterviewRecordList(interviewRecord);
    }

    /**
     * 新增模拟面试记录
     * 
     * @param interviewRecord 模拟面试记录
     * @return 结果
     */
    @Override
    public int insertInterviewRecord(InterviewRecord interviewRecord)
    {
        interviewRecord.setCreateTime(DateUtils.getNowDate());
        return interviewRecordMapper.insertInterviewRecord(interviewRecord);
    }

    /**
     * 修改模拟面试记录
     * 
     * @param interviewRecord 模拟面试记录
     * @return 结果
     */
    @Override
    public int updateInterviewRecord(InterviewRecord interviewRecord)
    {
        interviewRecord.setUpdateTime(DateUtils.getNowDate());
        return interviewRecordMapper.updateInterviewRecord(interviewRecord);
    }

    /**
     * 批量删除模拟面试记录
     * 
     * @param ids 需要删除的模拟面试记录主键
     * @return 结果
     */
    @Override
    public int deleteInterviewRecordByIds(Long[] ids)
    {
        return interviewRecordMapper.deleteInterviewRecordByIds(ids);
    }

    /**
     * 删除模拟面试记录信息
     * 
     * @param id 模拟面试记录主键
     * @return 结果
     */
    @Override
    public int deleteInterviewRecordById(Long id)
    {
        return interviewRecordMapper.deleteInterviewRecordById(id);
    }

    /**
     * 生成面试记录
     * @param positionId
     * @param message
     * @return
     */
    @Override
    public Long generateRecord(Long positionId, String message) {
        //1.添加面试记录，拿到新的面试记录id
        //1.1 获取当前登录的微信用户id
       LoginUser loginUser = SecurityUtils.getLoginUser();
        //Long wxUserId=1L;
        InterviewRecord interviewRecord = new InterviewRecord();
        interviewRecord.setUserId(loginUser.getUserId());
        interviewRecord.setPositionId(positionId);
        //存入用户id和岗位id
        interviewRecordMapper.insertInterviewRecord(interviewRecord);
        //2.添加问题
        //2.1解析json,解析为我所需要的格式并装入deepSeekResponseData集合中
        DeepSeekResponseData data = JSONObject.parseObject(message, DeepSeekResponseData.class);
        //类型转换 得到list
        List<DeepQuestionVo> deepQuestionVoList = data.getQuestions();
        //用来装面试问题 方便存入表中
        List<InterviewQuestion> questions = new ArrayList<>();
        for(int i=0;i<deepQuestionVoList.size();i++){
            InterviewQuestion interviewQuestion = new InterviewQuestion();

            DeepQuestionVo deepQuestionVo = deepQuestionVoList.get(i);//其中的一个对象
            interviewQuestion.setRecordId(interviewRecord.getId());
            interviewQuestion.setQuestionNo((long) i);
            interviewQuestion.setContent(deepQuestionVo.getText());
            interviewQuestion.setQuestionPrompt(deepQuestionVo.getSample_answer());
            interviewQuestion.setCreateTime(DateUtils.getNowDate());
            //添加进集合
            questions.add(interviewQuestion);
        }
        //2.2批量插入
        interviewQuestionMapper.insertInterviewByList(questions);

        //3.返回面试记录id
        return interviewRecord.getId();
    }

    /**
     * 开始面试
     * @param recordId
     * @return
     */
    @Override
    public List<InterviewQuestion> start(Long recordId) {
        //根据面试记录id查询面试记录
        InterviewRecord interviewRecord = interviewRecordMapper.selectInterviewRecordById(recordId);
        //修改面试记录状态
        interviewRecord.setStatus("0");
        interviewRecord.setStartTime(new Date());
        interviewRecordMapper.updateInterviewRecord(interviewRecord);
        //查询面试题目
        InterviewQuestion interviewQuestion = new InterviewQuestion();
        interviewQuestion.setRecordId(interviewRecord.getId());
        List<InterviewQuestion> questions = interviewQuestionMapper.selectInterviewQuestionList(interviewQuestion);

        return questions;
    }
}
